Units of Measure
F#の機能
Measure型の定義
code:fs
<Measure> type m
<Measure> type kg
Measure型の使用
code:fs
let height = 1.8<m>
let weight = 60<kg>
単位の組み合わせの単位を定義できる
code:fs
<Measure> type N = kg m/sec^2
let force1 = 5.0<kg m/sec^2>
let force2 = 5.0<N>
force1 = force2 // true
同一のものはちゃんとtrueになる
単位の異なる加減演算はエラーになる
code:fs
3.0<foot> + 2.0<foot> // OK
3.0<foot> + 2.0 // error
genericな関数への対応
<_>として任意の単位を受け付ける
code:fs
let square (x:int<_>) = x * x
// test
square 10<foot> // OK  foot^2
square 10<sec> // OK
Units of measure | F# for fun and profit
F# for Fun and Profit
具体例がたくさん載っていてわかりやすい
https://qiita.com/adacola/items/b65752b678e81bc8e354
https://learn.microsoft.com/ja-jp/dotnet/fsharp/language-reference/units-of-measure
https://davefancher.com/2012/11/18/f-more-on-units-of-measure/
/mrsekut-book-97816805025/121